﻿using JX.Core;
using JX.Core.Entity;
using JX.Dapper;
using JX.Infrastructure.Common;
using JX.Infrastructure.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace JX.Application
{
	/// <summary>
	/// 数据库表：Company 的应用层服务接口实现类.
	/// </summary>
	public partial class CompanyServiceAppDapper : ServiceAppDapper<CompanyEntity>,ICompanyServiceAppDapper
	{
		/// <summary>
		/// 数据扩展表名称
		/// </summary>
		private const string DataOptionTableName = "U_CompanyText";
		#region 仓储接口
		private readonly IUnitOfWorkFactory _uowFactory;
		private readonly IClientRepositoryDapper _ClientRepository;
		/// <summary>
		/// 构造器注入
		/// </summary>
		/// <param name="repository"></param>
		/// <param name="ClientRepository"></param>
		/// <param name="uowFactory"></param>
		public CompanyServiceAppDapper(ICompanyRepositoryDapper repository,
			IClientRepositoryDapper ClientRepository, 
			IUnitOfWorkFactory uowFactory) : base(repository)
		{
			m_repository = repository;
			_ClientRepository = ClientRepository;
			_uowFactory = uowFactory;

		}
		#endregion

		#region 添加
		/// <summary>
		/// 添加公司与扩展
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public bool AddFull(CompanyEntity entity)
		{
			bool result = false;
			using (var uow = _uowFactory.Create())
			{
				if (m_repository.Add(entity) && AddDataOption(DataOptionTableName, entity))
				{
					result = uow.Commit();
				}
			}
			return result;
		}
		/// <summary>
		/// 添加公司与扩展
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public async Task<bool> AddFullAsync(CompanyEntity entity)
		{
			bool result = false;
			using (var uow = _uowFactory.Create())
			{
				if (await m_repository.AddAsync(entity) && await AddDataOptionAsync(DataOptionTableName, entity))
				{
					result = uow.Commit();
				}
			}
			return result;
		}
		#endregion

		#region 修改
		/// <summary>
		/// 修改公司与扩展
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public bool UpdateFull(CompanyEntity entity)
		{
			bool result = false;
			using (var uow = _uowFactory.Create())
			{
				if (m_repository.Update(entity) && UpdateDataOption(DataOptionTableName, entity))
				{
					result = uow.Commit();
				}
			}
			return result;
		}
		/// <summary>
		/// 修改公司与扩展
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public async Task<bool> UpdateFullAsync(CompanyEntity entity)
		{
			bool result = false;
			using (var uow = _uowFactory.Create())
			{
				if (await m_repository.UpdateAsync(entity) && await UpdateDataOptionAsync(DataOptionTableName, entity))
				{
					result = uow.Commit();
				}
			}
			return result;
		}
		#endregion

		#region 删除
		/// <summary>
		/// 删除企业及相关用户数据.
		/// 1、修改客户表的客户类型。
		/// 2、从公司中移出会员。
		/// 3、删除公司。
		/// 4、删除公司扩展。
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public bool DeleteFull(int id)
		{
			bool result = false;
			using (var uow = _uowFactory.Create())
			{
				var ClientID = Get(id).ClientID;
				if (m_repository.Delete(id) && DeleteDataOption(DataOptionTableName, id))
				{
					_ClientRepository.Update("ClientType = @ClientType", "ClientID = @ClientID", new { ClientType = 1, ClientID });
					RemoveUsers(id);
					result = uow.Commit();
				}
			}
			return result;
		}
		/// <summary>
		/// 删除企业及相关用户数据.
		/// 1、修改客户表的客户类型。
		/// 2、从公司中移出会员。
		/// 3、删除公司。
		/// 4、删除公司扩展。
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<bool> DeleteFullAsync(int id)
		{
			bool result = false;
			using (var uow = _uowFactory.Create())
			{
				var ClientID = Get(id).ClientID;
				if (await m_repository.DeleteAsync(id) && await DeleteDataOptionAsync(DataOptionTableName, id))
				{
					_ClientRepository.Update("ClientType = @ClientType", "ClientID = @ClientID", new { ClientType = 1, ClientID });
					RemoveUsers(id);
					result = uow.Commit();
				}
			}
			return result;
		}
		/// <summary>
		/// 删除企业及相关用户数据.
		/// 1、修改客户表的客户类型。
		/// 2、从公司中移出会员。
		/// 3、删除公司。
		/// 4、删除公司扩展。
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		public void DeleteFull(string ids)
		{
			if (DataValidator.IsValidId(ids))
			{
				foreach (string id in ids.Split(new char[] { ',' }))
				{
					int companyId = DataConverter.CLng(id);
					if (companyId != 0)
					{
						DeleteFull(companyId);
					}
				}
			}
		}
		/// <summary>
		/// 删除企业及相关用户数据.
		/// 1、修改客户表的客户类型。
		/// 2、从公司中移出会员。
		/// 3、删除公司。
		/// 4、删除公司扩展。
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		public async Task DeleteFullAsync(string ids)
		{
			if (DataValidator.IsValidId(ids))
			{
				foreach (string id in ids.Split(new char[] { ',' }))
				{
					int companyId = DataConverter.CLng(id);
					if (companyId != 0)
					{
						await DeleteFullAsync(companyId);
					}
				}
			}
		}
		#endregion

		#region 移出会员
		/// <summary>
		/// 从企业中移出会员
		/// </summary>
		/// <param name="companyId"></param>
		/// <returns></returns>
		public bool RemoveUsers(int companyId)
		{
			string strSQL = "update Users set CompanyID = 0, UserType = 0 where CompanyID = @CompanyID";
			return m_repository.ExeSQL(strSQL, new { CompanyID = companyId }) > 0;
		}
		/// <summary>
		/// 从企业中移出会员
		/// </summary>
		/// <param name="companyId"></param>
		/// <returns></returns>
		public async Task<bool> RemoveUsersAsync(int companyId)
		{
			string strSQL = "update Users set CompanyID = 0, UserType = 0 where CompanyID = @CompanyID";
			int count = await m_repository.ExeSQLAsync(strSQL, new { CompanyID = companyId });
			return count > 0;
		}
		#endregion

		#region 是否存在扩展数据
		/// <summary>
		/// 是否存在扩展数据
		/// </summary>
		/// <param name="id"></param>
		/// <param name="tableName"></param>
		/// <returns></returns>
		public bool IsExistDataOption(int id, string tableName = DataOptionTableName)
		{
			return m_repository.IsExistDataOption(id,tableName);
		}
		/// <summary>
		/// 是否存在扩展数据
		/// </summary>
		/// <param name="id"></param>
		/// <param name="tableName"></param>
		/// <returns></returns>
		public async Task<bool> IsExistDataOptionAsync(int id, string tableName = DataOptionTableName)
		{
			return await m_repository.IsExistDataOptionAsync(id, tableName);
		}
		#endregion

		#region 添加扩展数据
		/// <summary>
		/// 添加扩展数据:从entity.Fields中获取表列及列值信息
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="entity"></param>
		/// <returns></returns>
		public bool AddDataOption(string tableName, CompanyEntity entity)
		{
			return m_repository.AddDataOption(tableName, entity);
		}
		/// <summary>
		/// 添加扩展数据:从entity.Fields中获取表列及列值信息
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="entity"></param>
		/// <returns></returns>
		public async Task<bool> AddDataOptionAsync(string tableName, CompanyEntity entity)
		{
			return await m_repository.AddDataOptionAsync(tableName, entity);
		}
		#endregion

		#region 修改扩展数据
		/// <summary>
		/// 修改扩展表数据，不存在则添加:从entity.Fields中获取表列及列值信息
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="entity"></param>
		/// <returns></returns>
		public bool UpdateDataOption(string tableName, CompanyEntity entity)
		{
			return m_repository.UpdateDataOption(tableName, entity);
		}
		/// <summary>
		/// 修改扩展表数据，不存在则添加:从entity.Fields中获取表列及列值信息
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="entity"></param>
		/// <returns></returns>
		public async Task<bool> UpdateDataOptionAsync(string tableName, CompanyEntity entity)
		{
			return await m_repository.UpdateDataOptionAsync(tableName, entity);
		}
		#endregion

		#region 删除扩展数据
		/// <summary>
		/// 删除扩展数据
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="id"></param>
		/// <returns></returns>
		public bool DeleteDataOption(string tableName, int id)
		{
			return m_repository.DeleteDataOption(tableName, id);
		}
		/// <summary>
		/// 删除扩展数据
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<bool> DeleteDataOptionAsync(string tableName, int id)
		{
			return await m_repository.DeleteDataOptionAsync(tableName, id);
		}
		#endregion

		#region 得到扩展数据
		/// <summary>
		/// 根据主键得到扩展数据
		/// </summary>
		/// <param name="id"></param>
		/// <param name="writeAndRead"></param>
		/// <returns></returns>
		public DataTable GetDataOption(int id, WriteAndReadEnum writeAndRead = WriteAndReadEnum.None)
		{
			return m_repository.GetDataOption(id, writeAndRead);
		}
		/// <summary>
		/// 根据主键得到扩展数据
		/// </summary>
		/// <param name="id"></param>
		/// <param name="writeAndRead"></param>
		/// <returns></returns>
		public async Task<DataTable> GetDataOptionAsync(int id, WriteAndReadEnum writeAndRead = WriteAndReadEnum.None)
		{
			return await m_repository.GetDataOptionAsync(id, writeAndRead);
		}
		#endregion

		#region 得到公司成员
		/// <summary>
		/// 根据主键得到公司成员
		/// </summary>
		/// <param name="id">公司ID</param>
		/// <param name="writeAndRead"></param>
		/// <returns></returns>
		public DataTable GetMemberInfo(int id, WriteAndReadEnum writeAndRead = WriteAndReadEnum.None)
		{
			return m_repository.GetMemberInfo(id, writeAndRead);
		}
		/// <summary>
		/// 根据主键得到公司成员
		/// </summary>
		/// <param name="id">公司ID</param>
		/// <param name="writeAndRead"></param>
		/// <returns></returns>
		public async Task<DataTable> GetMemberInfoAsync(int id, WriteAndReadEnum writeAndRead = WriteAndReadEnum.None)
		{
			return await m_repository.GetMemberInfoAsync(id, writeAndRead);
		}
		#endregion
	}
}